package cn.android.weblog.api;

import com.alibaba.fastjson.JSON;

import net.minidev.json.JSONObject;

import org.jsoup.Connection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.android.weblog.domain.User;
import cn.android.weblog.redisdata.LOGIN;
import cn.android.weblog.util.HttpUtils;

public class UserAPI {

    /**
     * 修改密码
     * @param pass
     * @param newPass
     * @return
     */
    public Map<String,Object> changePassword(String pass,String newPass){
        Map<String,String> params=new HashMap<>();
        params.put("password",pass);
        params.put("newPass",newPass);
        Connection.Response post = null;
        try {
            post= HttpUtils.post("/user/change/"+ LOGIN.USER.getUserId(), JSONObject.toJSONString(params));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>)JSON.parse(post.body());
        return results;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    public Map<String,Object> update(User user){
        Map<String,String> params=new HashMap<>();
        params.put("userId",user.getUserId().toString());
        params.put("email",user.getEmail());
        params.put("username",user.getUsername());
        Connection.Response post = null;
        try {
            post= HttpUtils.post("/user/update", JSONObject.toJSONString(params));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> results =(Map<String,Object>)JSON.parse(post.body());
        return results;
    }

}
